package org.snail.common.dingding.media;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.client.ServiceFactory;
import com.dingtalk.open.client.api.model.corp.UploadResult;
import com.dingtalk.open.client.api.service.corp.MediaService;
import org.apache.http.HttpResponse;
import org.snail.common.dingding.exception.OApiException;
import org.snail.common.dingding.utils.HttpHelper;
import org.snail.common.dingding.Env;
import org.snail.common.dingding.auth.AuthHelper;
import org.snail.exception.BaseException;


import java.io.File;
import java.io.IOException;

/**
 * 管理多媒体文件..
 * https://open-doc.dingtalk.com/docs/doc.htm?source=search&treeId=373&articleId=104971&docType=1.
 */

public class MediaHelper {

    /**
     * 资源文件类型
     */
    public static final String TYPE_IMAGE = "image";
    public static final String TYPE_VOICE = "voice";
    public static final String TYPE_VIDEO = "video";
    public static final String TYPE_FILE = "file";

    public static final String TYPE_LINK = "link";


    public static class MediaUploadResult {
        public String type;
        public String media_id;
        public String created_at;
    }

    /**
     * 上传多媒体文件
     * <p>
     */
    public static UploadResult upload(String accessToken, String type, File file) throws Exception {

        MediaService mediaService = ServiceFactory.getInstance().getOpenService(MediaService.class);
        UploadResult uploadResult = mediaService.uploadMediaFile(accessToken, type, file);
        return uploadResult;
    }

    /**
     *  调用上面的接口发不了文件，下面通过http尝试发送
     */
    public static void DDUpFile(String jsonObj) throws IOException, BaseException, OApiException {
        String url = Env.OAPI_HOST+"/chat/send";
        HttpHelper.DDUpFile(AuthHelper.getAccessToken(),url,jsonObj);

    }

    /**
     * 钉钉http链接发送
     * @param jsonObj
     * @throws BaseException
     * @throws OApiException
     * @throws IOException
     */
    public static void DDLinkSend(String jsonObj) throws BaseException, OApiException, IOException {
        String url = Env.OAPI_HOST+"/chat/send";
        HttpHelper.DDLinkSend(AuthHelper.getAccessToken(),url,jsonObj);
    }


    /**
     * 下载多媒体文件，目前sdk没有封装此接口，需要通过HTTP访问.
     */
    public static void download(String accessToken, String mediaId, String fileDir) throws Exception {
        String url = Env.OAPI_HOST + "/media/downloadFile?" +
                "access_token=" + accessToken + "&media_id=" + mediaId;
        JSONObject response = HttpHelper.downloadMedia(url, fileDir);
        System.out.println(response);
    }

    /**
     * 根据文件获取mediaId
     * @param accessToken
     * @param mediaType
     * @param file
     * @return
     * @throws OApiException
     * @throws IOException
     */
    public static HttpResponse getMediaId(String accessToken,String mediaType,File file) throws OApiException, IOException {
        String url = Env.OAPI_HOST +"/media/upload";
        HttpResponse httpResponse = HttpHelper.getMediaId(url,accessToken,mediaType,file);
        return httpResponse;
    }

    /**
     * 下载文件
     * @param accessToken
     * @param mediaId
     * @throws OApiException
     * @throws IOException
     */
    public static void downLoadByMediaId(String accessToken,String mediaId) throws OApiException, IOException {
        String url = Env.OAPI_HOST +"/media/downloadFile";
        HttpHelper.downLoadByMediaId(url,accessToken,mediaId);

    }

}
